<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 *  File : listModules.php
 *  called only via ajax 
 *  List all the courses so we can enroll a student in the course
 */  
 
checkSession();
if ($session->userType() > 2) // admin, head master
{
  die('Wrong user rights');
  exit;
}
require '../../includes/classes/WS3tableview.php';

$user_id=$_POST['id'];

// check if there is such a lesson
$dbUser=new WS3_Database();
$q="SELECT `id`,`last_name` FROM `user` WHERE `id`='$user_id' LIMIT 1";

if ( $dbUser->query($q) &&  $dbUser->nextRecord() )
{
 //$$TODO
}

$table=new WS3_TableView('main',400);
$table->setTitle( 'Add Student to course '.javaSafeString( $dbUser->f('last_name') ) );
$table->addColumnTH('Name');
$table->addColumn('Add','addCourseStudentUpdate&amp;user_id='.$user_id);
$table->setAlternativeRows(true);
$table->setInfoRow(true);


// pull the courses from the database
$db=new WS3_Database();
/**
 SELECT c.id as cid, c.name AS cname, u2c.course_id AS u2ccourse_id FROM `user2course` AS u2c, course AS c  WHERE (u2c.user_id = '6') AND NOT( c.id = u2c.course_id )
 */

  
// to do select only courses for which the student is not already enrolled  
 $q="SELECT DISTINCT c.id as cid, c.name AS cname
  FROM course AS c
  WHERE c.id
  NOT IN ( SELECT course_id FROM  user2course WHERE
  user_id = '$user_id'  )
  "; // 


if ($db->query($q))
{
  $count = $db->numberRows();
  while ( $db->nextRecord() )
  {
     $table->addRow($db->f('cid'), array( 
     'Name' => javaSafeString( $db->f('cname') )
      ));
  } 
  $message="Listing";
}else{
  $message="Error : Empty list";
}
 
  $table->addToInfoRow("<p>Count = $count </p>");
  echo $table->display();

/** Get menu */
require '../../includes/menus/topCoursesStudent.php';
$menu=javaSafeString($menu);
echo "document.getElementById('topmenu').innerHTML='$menu';";

echo "document.getElementById('messages').innerHTML='$message';";
if (W3DEBUG)  $debug=new WS3_Debug();

?>